#region File Description
//-----------------------------------------------------------------------------
// Camera.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
#endregion

namespace TimeWar
{
    public class Camera
    {

        private float aspectRatio = 16 / 9;
        private float viewAngle = MathHelper.PiOver4;
        //private float viewAngle = MathHelper.PiOver2;

        private float nearClip = 0.00001f;
        private float farClip = 20000.0f;

        /// <summary>
        /// A global projection matrix since it never changes
        /// </summary>
        private Matrix projection;

        /// <summary>
        /// A global view matrix since it never changes
        /// </summary>
        private Matrix view;

        private Matrix rotationMatrix;

        /// <summary>
        /// The Camera position which never changes
        /// </summary>
        private Vector3 cameraPosition;

        // Camera direction
        // Create a vector pointing the direction the camera is facing.
        private Vector3 transformedReference;

        private Vector3 thirdPersonReference = new Vector3(0, 200, -200);

        protected void updateView(Quaternion objRotation, Vector3 objPosition)
        {


            //view = Matrix.CreateLookAt(viewPosition, Vector3.Zero, Vector3.Forward);
            //viewPosition = Vector3.Transform(viewPosition, Matrix.CreateFromQuaternion(objRotation));
            //viewPosition = Vector3.Transform(viewPosition, Matrix.CreateTranslation(objPosition));
            //Vector3 camup = new Vector3(0, 0, 1);
            //camup = Vector3.Transform(camup, Matrix.CreateFromQuaternion(objRotation));
            //view = Matrix.CreateLookAt(viewPosition, objPosition, camup);
            ////view = Matrix.CreateLookAt(viewPosition, Vector3.Zero, Vector3.Forward);
            //projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)16 / 9, 0.2f, 500.0f);

            rotationMatrix = Matrix.CreateRotationY(objRotation.W);
            transformedReference = Vector3.Transform(thirdPersonReference, rotationMatrix);
            cameraPosition = transformedReference + objPosition;
            //view = Matrix.CreateLookAt(cameraPosition, objPosition, new Vector3(0.0f, 1.0f, 0.0f));
            view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Forward);
            projection = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio, nearClip, farClip);
        }

        #region Properties
        public Matrix Projection
        {
            get
            {
                return projection;
            }
        }

        public Matrix View
        {
            get
            {
                return view;
            }
        }

        public Vector3 CameraPosition
        {
            get
            {
                return cameraPosition;
            }
            set
            {
                cameraPosition = value;
                //updateView();
            }
        }

        public float AspectRatio
        {
            get
            {
                return aspectRatio;
            }
            set
            {
                aspectRatio = value;
            }
        }

        public float ViewAngle
        {
            get
            {
                return viewAngle;
            }
            set
            {
                viewAngle = value;
            }
        }

        public float NearClip
        {
            get
            {
                return nearClip;
            }
            set
            {
                nearClip = value;
            }
        }

        public float FarClip
        {
            get
            {
                return farClip;
            }
            set
            {
                farClip = value;
            }
        }


        #endregion

        public Camera()
        {
        }

        public Camera(Quaternion playerRotation, Vector3 playerPosition)
        {
            updateView(playerRotation, playerPosition);
        }

        public void Update(Vector3 playerPosition, Quaternion playerRotation)
        {
            if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y < 0)
            {
                cameraPosition.Z += 100;
            }
            else if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y > 0)
            {
                cameraPosition.Z -= 100;
            }

            if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X < 0)
            {
                cameraPosition.X += 100;
            }
            else if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X > 0)
            {
                cameraPosition.X -= 100;
            }

            updateView(playerRotation, playerPosition);


        }

    }
}

